$(function () {
    $("#backPage").load("/html/backPage.html");
    getTeamList();
})
// 查询所有用户
function findAll() {
    let data = {
        key: $("#selectKey").val(),
        startTime: $("#startTime").val(),
        endTime: $("#endTime").val(),
        team: $.trim($("#selectTeam").val()),
        education: $.trim($("#selectEducation").val()),
        state: $.trim($("#selectState").val()),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    console.log(data)
    let result = jsonGetValue("/user/getUserList", data);
    console.log(result);
    setAllData(result.list);
    $("#total").html(result.count);
}
// 给用户列表赋值
function setAllData(data) {
    let html = "";
    for (let i=0; i<data.length;i++) {
        html += "<tr>\n" +
            "        <td>"+ data[i].id +"</td>\n" +
            "        <td>"+ data[i].userCode +"</td>\n" +
            "        <td>"+ (data[i].name == undefined?"暂无":data[i].name)  +"</td>\n" +
            "        <td>"+ (data[i].sexName == undefined?"暂无":data[i].sexName) +"</td>\n" +
            "        <td>"+ (data[i].teamName == undefined?"暂无":data[i].teamName) +"</td>\n" +
            "        <td>"+ (data[i].educationName == undefined?"暂无":data[i].educationName) +"</td>\n" +
            "        <td>"+ (data[i].college == undefined?"暂无":data[i].college) +"</td>\n" +
            "        <td>"+ (data[i].address == undefined?"暂无":data[i].address) +"</td>\n" +
            "        <td>"+ (data[i].phone == undefined?"暂无":data[i].phone) +"</td>\n" +
            "        <td>"+ (data[i].urgentPeople == undefined?"暂无":data[i].urgentPeople) +"</td>\n" +
            "        <td>"+ data[i].updateTime +'</td>\n';
        if (data[i].state == "normal") {
            html += "<td class='yes'><div onclick=\"changeShowState('" + data[i].userCode + "','frozen')\"><span>"+ data[i].userState +"</span><span class='iconfont'>&#xe60d</span></div></td>\n";
        } else if (data[i].state == "frozen") {
            html += "<td class='no'><div onclick=\"changeShowState('" + data[i].userCode + "','normal')\"><span>"+ data[i].userState +"</span><span class='iconfont'>&#xe600</span></div></td>\n";
        }
    }

    $("#tbody").html(html)
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    $("#tbody > tr > td:nth-child(12) > div").css({"cursor": "pointer","display": "flex","align-items": "center"})

}

// 修改用户状态
function changeShowState(userCode,state){
    let data = {
        userCode: userCode,
        state:state
    };
    let result = jsonPost("/user/updateState", data)
    if (result.result == 1) {
        page('go');
    } else {
        alert("修改状态失败");
    }
}

// 获取小组
function getTeamList() {
    let result = jsonGet("/team/getTeamList");
    console.log(result.data);
    setTeamListData(result.data);
}
// 给小组下拉列表赋值
function setTeamListData(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].teamCode +'">'+ data[i].teamName +'</option></div>';
    }
    $("#selectTeam").html(html);
    getEducationByCode();
}

// 从码表获取学历
function getEducationByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"education"});
    console.log(result.data);
    setEducationDataByCode(result.data);
}
// 给学历下拉列表赋值
function setEducationDataByCode(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].code +'">'+ data[i].key +'</option></div>';
    }
    $("#selectEducation").html(html);
    getStateByCode();
}

// 从码表获取状态
function getStateByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"user_state"});
    console.log(result.data);
    setStateDataByCode(result.data);
}
// 给状态下拉列表赋值
function setStateDataByCode(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].code +'">'+ data[i].key +'</option></div>';
    }
    $("#selectState").html(html);
    findUserByUserCode();
}

// 查询点击事件
function select() {
    findAll();
}
// 清空点击事件
function clearAll() {
    $("input[ type='text']").val("");
    $("input[ type='date']").val("");
    $("#selectTeam").val("all");
    $("#selectEducation").val("all");
    $("#selectState").val("all");
    $("#pageSize").val(5);
    $("#pageNum").val(1);
}